<?php

namespace app\common\model\system;

use app\common\model\BaseModel;
use app\common\service\AuthService;

class Menu extends BaseModel
{
    protected $name = 'system_menu';

    public function getTree($user_id = 1,$pid = 0){
        $treeList = [];
        $menuList = $this->getList(['status'=>1],'id asc');
        $authService = app(AuthService::class,['user_id' =>$user_id]);
        foreach ($menuList as $v){
            $check = empty($v['path'])?true:$authService->checkNode($v['path']);
            if($v['pid'] == $pid && $check){
                $v['children']= $this->getTree($user_id,$v['id']);
                if(!$v['children']){
                    unset($v['children']);
                }
                $treeList[] = $v;
            }
        }
        return $treeList;
    }

}